package com.pig4cloud.pigx.ruralLibrary.util;

import java.math.BigDecimal;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

public class CommonUtil {
	private static CommonUtil singletonLazy;

	private CommonUtil() {

	}
	public static String convertMD5(String inStr) {

		char[] a = inStr.toCharArray();
		for (int i = 0; i < a.length; i++) {
			a[i] = (char) (a[i] ^ 't');
		}
		String s = new String(a);
		return s;

	}

	public static CommonUtil getInstance() {
		try {
			if (null == singletonLazy) {
				// 模拟在创建对象之前做一些准备工作
				Thread.sleep(1000);
				synchronized (RandomCodeUtil.class) {
					if (null == singletonLazy) {
						singletonLazy = new CommonUtil();
					}
				}
			}
		} catch (InterruptedException e) {
			// TODO: handle exception
		}
		return singletonLazy;
	}

	/**
	 * 解析obj成long
	 * 
	 * @param obj
	 * @return
	 */
	public long praseToLong(Object obj) {
		try {
			if (obj == null) {
				return 0l;
			}
			long longValue = (long) obj;
			return longValue;
		} catch (Exception e) {
			System.out.println("long 转换出错：" + e.getMessage());
			return 0l;
		}
	}

	/**
	 * 解析obj成long
	 * 
	 * @param obj
	 * @return
	 */
	public BigDecimal praseToDecimal(Object obj) {
		try {
			if (obj == null) {
				return BigDecimal.ZERO;
			}
			BigDecimal strValue = (BigDecimal) obj;
			return strValue;
		} catch (Exception e) {
			System.out.println("BigDecimal转换出错：" + e.getMessage());
			return BigDecimal.ZERO;
		}
	}

	public static String getCode() {
		return (int) ((Math.random() * 9 + 1) * 100000) + "";
	}

	private static long tmpID = 0;

	private static boolean tmpIDlocked = false;

	public static long getId() {
		long ltime = 0;
		while (true) {
			if (tmpIDlocked == false) {
				tmpIDlocked = true;
				// 当前：（年、月、日、时、分、秒、毫秒）*10000
				ltime = Long.valueOf(new SimpleDateFormat("yyMMddhhmmssSSS").format(new Date()).toString()) * 10000;
				if (tmpID < ltime) {
					tmpID = ltime;
				} else {
					tmpID = tmpID + 1;
					ltime = tmpID;
				}
				tmpIDlocked = false;
				return ltime;
			}
		}
	}

	/**
	 * 生成 uuid， 即用来标识一笔单，也用做 nonce_str
	 * @return
	 */
	public static String generateUUID(){
		String uuid = UUID.randomUUID().toString().
				replaceAll("-","").substring(0,32);

		return uuid;
	}


	/**
	 * md5常用工具类
	 * @param data
	 * @return
	 */
	public static String MD5(String data){
		try {

			MessageDigest md5 = MessageDigest.getInstance("MD5");
			byte [] array = md5.digest(data.getBytes("UTF-8"));
			StringBuilder sb = new StringBuilder();
			for (byte item : array) {
				sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
			}
			return sb.toString().toUpperCase();

		}catch (Exception e){
			e.printStackTrace();
		}
		return null;

	}


	public static void main(String[] args) {
		System.out.println(CommonUtil.getId());
		System.out.println(CommonUtil.getId());
		System.out.println(CommonUtil.getId());
		System.out.println(CommonUtil.getId());
		System.out.println(CommonUtil.getId());
		System.out.println(CommonUtil.getId());
	}
	
	 
}
